Adding some more judges, here and there.
[andmenj-acm.git] / ICPC Live Archive / 3928 - Ballroom lights / help / gomox.ar-pap-2010-d9b17e5cb110 / tpb / doc / acm.tex
blobfdd73652ebba53d782b54d2273acef76ff7b6656
1 \newcommand{\tq}{\ensuremath{\ |\ }}
2 \section{10600-ACM Contest and Blackout}
3 \textbf{Problema:} Dado un conjunto de escuelas, y posibles conexiones entre ellas, cada una con un costo, encontrar los dos costos m\'as baratos para conectarlos.
5 \subsection{Resolución}
6 El problema se modela como un problema de grafos. El grafo tiene un nodo por cada escuela y existe un eje entre dos nodos por cada forma de conectar las escuelas representadas por esos nodos. El costo del eje es el costo de la conexión. En este modelo la respuesta consiste en buscar el costo de los dos árboles m\'as baratos. M\'as formalmente, si $g$ es el grafo, consideramos el siguiente conjunto $A$:
8 $A=\{ T \tq T$ es árbol generador de $g$$\}$
10 lo que buscamos es $costo(T_1),costo(T_2)$ tal que:
12 $T_1 \in A \wedge T_2 \in A \wedge T_1 \neq T_2 \wedge T_1$ es árbol generador mínimo $\wedge \forall T_3 \in A, T_1 \neq T_3 \implies costo(T_2) \leq costo(T_3)$
14 Dado que el problema nos dice que podemos encontrar los costos, podemos asumir que el grafo tiene m\'as de una forma de conectar a todos los nodos.
16 Si tenemos la forma m\'as barata de conectar a todas las escuelas y miramos
17 esta solución en el grafo, vemos que tiene que ser un árbol generador mínimo:
18 es un \'arbol porque no puede haber escuelas ``sueltas'' (es conexo) y
19 no puede haber ciclos, ya que las
20 conexiones tienen un costo positivo (por lo cual eliminar una de esas
21 conexiones abaratar\'ia el costo). Adem\'as, tiene
22 que ser mínimo, ya que se busca que sea el de mínimo costo. La segunda forma
23 m\'as barata de conectar a las escuelas tiene que ser también un árbol generador,
24 ya que, por los mismos motivos de antes, el grafo correspondiente debe ser
25 conexo y sin ciclos. Por lo tanto, si hay una solución al problema, sus costos
26 tienen que coincidir con los costos de $T_1$ y $T_2$.
28 Por otro lado es simple ver que los costos de $T_1$ y de $T_2$ son solución al
29 problema, ya que si alguno de los dos no fuera es porque hay una forma m\'as
30 barata de conectarlos; pero eso es absurdo por la definición de $T_1$ y de
31 $T_2$.
33 Encontrar $T_1$ es simple, se puede usar el algoritmo de Kruskal para hallar un
34 árbol generador mínimo.
35 Para encontrar $T_2$ podría sacarse uno a uno los ejes que están en $T_1$ de $g$,
36 y buscar el árbol generador mínimo de los sucesivos $g'$ hasta hallar el mas
37 chico de todos ellos. Sin embargo esto es muy costoso y puede hacerse mejor.
38 Para ello, usamos el Teorema~\ref{t1} demostrado abajo, que nos permite
39 asegurar que se puede encontrar un $T_2$ cambiando a lo sumo un eje de $T_1$.
41 Sean dos ejes $(u,v) \in T_1$, $(x,y) \notin T_1$ tales que $T_1\cup \{(x,y)\}$
42 tiene un ciclo en que tambi\'en est\'a el eje $(u,v)$. Sea adem\'as
43 $T_2=T_1-\{(u,v)\}\cup\{(x,y)\}$, entonces:
45 %TODO: esto les parece que hay que justificarlo mas?
46 %RESPUESTA: naaah
47 $costo(T_2) = costo(T_1) - costo((u,v)) + costo((x,y))$
49 Si queremos minimizar el costo de $T_2$ dado un $(x,y)$ fijo, debemos buscar cual es
50 el $(u,v)$ que minimiza $- costo((u,v)) + costo((x,y))$. Este $(u,v)$ es el eje
51 m\'as pesado del único camino que existe en $T_1$ entre $x$ e $y$. Una forma de
52 resolver el problema es, para cada eje $(x,y)$ que ocurr\'ia en el grafo original
53 pero qued\'o excluido de $T_1$, calcular cu\'al es el eje $(u,v)$ que le corresponde.
54 Una vez hecho esto, se tienen los posibles mínimos incrementos que genera agregar
55 un eje a $T_1$. El \'arbol $T_2$ buscado es el que minimiza este incremento.
57 %los posibles mínimos incrementos que genera agregar
58 %un eje a $T_1$. El costo que buscamos es el de $T_1$ mas el mínimo de estos incrementos.
60 Una forma de hacer esto es encontrar, usando DFS, el eje m\'as caro del
61 camino entre $u$ y $v$ para todo par de nodos $u,v$. El camino es \'unico
62 porque $T_1$ es un árbol.
64 \begin{algorithm}[H]
65 \begin{algorithmic}
66 \caption{Encontrar el eje m\'as caro en el camino que existe en un árbol para cada par de nodos $u$, $v$}
67 \PARAMS{árbol sobre el que buscar}
68 \STATE $M_{v,w} :=$ no calculado $\forall v, w$ nodos del árbol
69 \FOR{cada nodo $v$ en el árbol}
70 \STATE \texttt{llenar\_con\_dfs\_m\'ax}($v$, $v$, $max$)
71 \ENDFOR
72 \end{algorithmic}
73 \end{algorithm}
75 \begin{algorithm}[H]
76 \begin{algorithmic}
77 \caption{\texttt{llenar\_con\_dfs\_m\'ax:} Encontrar los ejes m\'as caros para los caminos que empiezan en un nodo}
78 \PARAMS{nodo inicial $u$, otro nodo $x$, matriz ($max$) donde guardar los resultados}
79 \FOR{cada nodo $v$ adyacente a $x$}
80 \IF{$M_{u,v}$ es no calculado y $v\neq u$}
81 \IF{$x=u$ $\lor$ $peso(u,v) > M_{u,x}$}
82 \STATE $M_{u,v} := (x,v)$
83 \ELSE
84 \STATE $M_{u,v} := M_{u,x}$
85 \ENDIF
86 \STATE \texttt{llenar\_con\_dfs\_m\'ax}($u$, $v$, $max$)
87 \ENDIF
88 \ENDFOR
89 \end{algorithmic}
90 \end{algorithm}
92 Para obtener el árbol generador mínimo se utilizó el algoritmo de Kruskal, con una estructura auxiliar para {\em union-find}
93 representada con un bosque de conjuntos disjuntos, de manera tal que cada nodo mantiene un puntero al representante de su
94 clase. El algoritmo para la resolución del problema fue:
96 \begin{algorithm}[H]
97 \begin{algorithmic}
98 \caption{Costo de los dos \'arboles generadores m\'as baratos}
99 \PARAMS{un grafo $g$ con peso en los ejes}
100 \STATE ordenar los ejes según su peso
101 \STATE obtener el árbol generador mínimo $T_1$ y su costo mediante el algoritmo de Kruskal
102 \STATE $M_{u,v}$ := eje m\'as caro en el camino desde $u$ hasta $v$ en $T_1$
103 \STATE costo nuevo $:= \infty$
104 \FOR{cada eje $(u,v)$ que quedo fuera del árbol generador mínimo}
105 \IF{costo nuevo $>$ $costo(T_1) - costo(M_{u,v}) + costo(u,v)$}
106 \STATE costo nuevo := $costo(T_1) - costo(M_{u,v}) + costo(u,v)$
107 \ENDIF
108 \ENDFOR
109 \RETURN $\langle$ $costo(T_1)$, costo nuevo $\rangle$
110 \end{algorithmic}
111 \end{algorithm}
113 Podemos analizar la complejidad en base al pseudoc\'odigo antes presentado.
114 Lo primero que hacemos es ordenar los ejes, lo cual toma $O(E \log E)$\footnote{Eventualmente se podría mejorar la complejidad utilizando bucket sort, ya que el costo de cada eje es a lo sumo 300.}, que es igual a $O(E \log V$). Luego obtenemos el árbol generador mínimo. Como usamos Kruskal con {\em union-find} y los ejes ya están ordenados, la complejidad es $O(E \alpha(V))$.
116 Luego llenamos la matriz $M$, que como vimos antes, se logra haciendo DFS desde
117 cada nodo, lo cual nos da un costo $O(V^2)$, ya que se hacen $V$ recorridos sobre el
118 árbol, cada uno de los cuales cuesta $O(V)$ porque, como es un árbol, tiene $V-1$ ejes. En
119 la implementación, no se llenan todas las posiciones $M_{u,v}$ al inicio, sino que
120 se hace cada DFS a medida que se necesita su resultado. Esto no cambia la
121 complejidad en peor caso, pero mejora los casos en los que hay pocos ejes.
122 Finalmente, recorremos los ejes que no est\'an en el árbol, buscando cu\'al es el
123 incremento mínimo que se produce al agregarlo; esto se hace en $O(E)$.
124 Por lo tanto, la complejidad temporal es $O(V^2 + E \log V)$.
126 \newtheorem{theorem}{Teorema}
127 \begin{theorem}
128 \label{t1}
129 Sea $g$ un grafo tal que posee por lo menos dos árboles generadores. Entonces
130 existe un árbol generador $T'$ tal que si $T$ es un árbol generador mínimo,
131 entonces $T'$ difiere de $T$ en exactamente un eje y para todo otro árbol
132 generador $T''$ ($T'' \neq T$), $costo(T') \leq costo(T'')$.
133 \end{theorem}
134 \begin{proof}
135 Veamos primero que existe un \'arbol generador que difiere de $T$ en exactamente un
136 eje.
138 Sabemos que $g$ tiene por lo menos dos árboles generadores, por lo cual existe
139 por lo menos un eje en g que no pertenece a $T$. Sea $e$ un eje de $g$ que no
140 est\'a en $T$. Si se agrega $e$ a $T$, este pasa a tener un ciclo. Si se saca
141 un eje de ese ciclo, que no sea $e$, se obtiene otro árbol generador que
142 difiere de $T$ en exactamente un eje.
144 Sea $T'$ un árbol generador que difiere de $T$ en exactamente un eje y cuyo
145 costo es mínimo entre todos los que se diferencian de $T$ en exactamente un
146 eje. Es decir:
148 $A =\{t \tq t \text{ es árbol generador} \land \exists e \in E . T - t = \{e\}\}$ (difieren de $T$ en un eje)
150 $T' \in \{ t \tq t \in A \wedge \forall t_1 \in A, costo(t) \leq costo(t_1)\}$
152 Veamos que $costo(T') \leq costo(T'')$ para todo \'arbol generador $T'' \neq T$.
154 Por la elecci\'on de $T'$, s\'olo falta considerar aquellos \'arboles generadores
155 $T''$ que difieran de $T$ en dos o m\'as ejes.
156 Supongamos que existe $T''$ que difiere de $T$ en dos o m\'as ejes y que
157 $costo(T') > costo(T'')$.
158 Considerar alg\'un $T''$ de costo mínimo y, dentro de los de costo m\'inimo,
159 el que difiera de $T$ en la menor cantidad de ejes. M\'as formalmente:
160 %$B =\{t \tq t $ es árbol generador,$ \#(T-t) \geq 2\}$
162 $C =\{t \tq t \text{ es \'arbol generador }\land \#(T - t) \geq 2 \land costo(t) < costo(T')\}$ (difieren de $T$ en 2 o m\'as ejes)
164 \noindent queremos ver que $C=\emptyset$. Consideramos:
166 $C' = \{ t \tq t \in C \wedge \forall t_1 \in C, costo(t) \leq costo(t_1)\}$ (de costo m\'inimo)
168 $C'' = \{ t \tq t \in C' \wedge \forall t_1 \in C', \#(T-t) \leq \#(T-t_1)\}$ (difieren de $T$ en m\'inima cantidad de ejes)
170 Probar que $C=\emptyset$ es equivalente a probar que $C''=\emptyset$. Supongamos que $C'' \neq \emptyset$, entonces, sea $T'' \in C''$.
172 Existe un eje $(u,v) \in T-T''$ que adem\'as tiene costo mínimo entre todos los que pertenecen a $T-T''$. Si agregamos $(u,v)$ a $T''$ obtenemos un ciclo, y en este ciclo hay un eje $(x,y)\in T''-T$ (porque si no habr\'ia un ciclo en $T$).
174 \begin{itemize}
175 \item Si $peso((u,v)) > peso((x,y))$, agregamos $(x,y)$ a $T$ y obtenemos un ciclo. Hay un eje $(u',v')$
176 en este ciclo que cumple que $(u',v') \in T-T''$ (porque si no habr\'ia un ciclo en $T''$).
177 Como $(u,v)$ era el mínimo de todos los ejes de $T-T''$, vale que $peso((u,v))\leq peso((u',v'))$.
179 Si $peso((u,v)) \leq peso((u',v'))$ y consideramos $T^\star=T-\{(u',v')\}\cup\{(x,y)\}$ obtenemos un árbol generador que tiene un costo mayor o igual que el de $T$.
181 En verdad, no puede ser igual, porque $T^\star$ difiere de $T$ en exactamente
182 un eje, y tendríamos que $costo(T) = costo(T^\star) \geq costo(T') > costo(T'')$
183 y esto es absurdo ya que $T$ tiene costo mínimo.
185 Entonces $T^\star$ tiene un costo mayor, por lo cual tiene que valer que
186 $peso((u',v')) < peso((x,y))$, entonces $peso((u',v')) < peso((x,y)) < peso((u,v))$. Esto es absurdo, ya que $(u,v)$ era un eje de peso mínimo en $T-T''$
188 %Vemos entonces que $peso((u,v))\leq peso((x,y))$.
190 \item
191 Si $peso((u,v)) = peso((x,y))$, resulta que existe $T^\star = T''-\{(x,y)\}\cup \{(u,v) \}$
192 que tiene el mismo costo que $T''$ pero difiere de $T$ en menos ejes.
193 Entonces, o bien $T^\star$ difiere en exactamente un eje de $T$, lo cual
194 es absurdo porque $costo(T'') < costo(T') \leq costo(T^\star) = costo(T'')$,
195 o bien difiere en m\'as de un eje,
196 pero esto no puede ser porque, dentro de los de m\'inimo costo, $T''$ difer\'ia
197 de $T$ en m\'inima cantidad de ejes.
198 %pero esto no puede ser porque entonces $T^\star$ diferir\'ia en un eje menos de $T$ que $T''$.
200 \item Por \'ultimo, si $peso((u,v)) < peso((x,y))$, entonces $T''-\{(x,y)\}\cup \{(u,v) \}$ pesa
201 menos que $T''$ y difiere de $T$ en menos ejes, lo cual es nuevamente absurdo por
202 las asunciones hechas sobre $T''$.
203 \end{itemize}
205 En todos los casos, el absurdo provino de suponer que existía $T''$.
206 \end{proof}
209 \subsection{Implementación}
210 \noindent
211 \ttfamily
212 \shorthandoff{"}\\
213 \hlstd{}\hlline{\ 1\ }\hldir{\#include$<$iostream$>$}\\
214 \hlline{\ 2\ }\hlstd{}\hldir{\#include\ $<$stdio.h$>$}\\
215 \hlline{\ 3\ }\hlstd{}\hldir{\#include\ $<$stdlib.h$>$}\\
216 \hlline{\ 4\ }\hlstd{}\hldir{\#include\ $<$vector$>$}\\
217 \hlline{\ 5\ }\hlstd{}\hldir{\#include\ $<$algorithm$>$}\\
218 \hlline{\ 6\ }\hlstd{}\hldir{\#include\ $<$list$>$}\\
219 \hlline{\ 7\ }\hlstd{}\\
220 \hlline{\ 8\ }\hlkwa{using\ namespace\ }\hlstd{std}\hlsym{;}\\
221 \hlline{\ 9\ }\hlstd{}\hldir{\#define\ foreachin(it,s)\ for\ (typeof(s.begin())\ it\ =\ (s).begin();\ it\ !=\ (s).end();\ it++)}\\
222 \hlline{10\ }\hlstd{}\hldir{\#define\ forn(i,n)\ for(unsigned\ short\ i\ =\ 0;\ i\ $<$\ (n);\ i++)}\\
223 \hlline{11\ }\hlstd{}\\
224 \hlline{12\ }\\
225 \hlline{13\ }\hlkwc{class\ }\hlstd{uf\textunderscore node}\hlsym{\{}\\
226 \hlline{14\ }\hlstd{}\hlkwc{public}\hlstd{}\hlsym{:}\\
227 \hlline{15\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{name}\hlsym{;}\\
228 \hlline{16\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{rank}\hlsym{;}\\
229 \hlline{17\ }\hlstd{uf\textunderscore node\ }\hlsym{{*}}\hlstd{parent}\hlsym{;}\\
230 \hlline{18\ }\hlstd{}\hlsym{\};}\\
231 \hlline{19\ }\hlstd{}\\
232 \hlline{20\ }\\
233 \hlline{21\ }\hlkwc{class\ }\hlstd{union\textunderscore find}\hlsym{\{}\\
234 \hlline{22\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwc{public\ }\hlstd{}\hlsym{:}\\
235 \hlline{23\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{S}\hlsym{;}\\
236 \hlline{24\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwc{inline\ }\hlstd{}\hlkwd{union\textunderscore find}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{n}\hlsym{)\{}\\
237 \hlline{25\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{S\ }\hlsym{=}\hlstd{}\hlkwa{new\ }\hlstd{uf\textunderscore node}\hlsym{{[}}\hlstd{n}\hlsym{{]};}\\
238 \hlline{26\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{for}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{i}\hlsym{=}\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\hlstd{i}\hlsym{$<$}\hlstd{n}\hlsym{;}\hlstd{i}\hlsym{++)\{}\\
239 \hlline{27\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{S}\hlsym{{[}}\hlstd{i}\hlsym{{]}.}\hlstd{rank}\hlsym{=}\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
240 \hlline{28\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{S}\hlsym{{[}}\hlstd{i}\hlsym{{]}.}\hlstd{name}\hlsym{=}\hlstd{i}\hlsym{;}\\
241 \hlline{29\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{S}\hlsym{{[}}\hlstd{i}\hlsym{{]}.}\hlstd{parent}\hlsym{=\&}\hlstd{S}\hlsym{{[}}\hlstd{i}\hlsym{{]};}\\
242 \hlline{30\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
243 \hlline{31\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
244 \hlline{32\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{$\sim$}\hlstd{}\hlkwd{union\textunderscore find}\hlstd{}\hlsym{()\{}\hlstd{}\hlkwa{delete}\hlstd{}\hlsym{{[}{]}\ }\hlstd{S}\hlsym{;\}}\\
245 \hlline{33\ }\hlstd{\\
246 \hlline{34\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwc{inline\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{Union}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{a}\hlsym{,\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{b}\hlsym{)\{\ }\hlstd{}\hlslc{//use\ Union\ because\ union\ is\ a\ keyword\ :P}\\
247 \hlline{35\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{ranka}\hlsym{,}\hlstd{rankb}\hlsym{;}\\
248 \hlline{36\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{ranka}\hlsym{=}\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]}.}\hlstd{parent}\hlsym{{-}$>$}\hlstd{rank}\hlsym{;}\\
249 \hlline{37\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{rankb}\hlsym{=}\hlstd{S}\hlsym{{[}}\hlstd{b}\hlsym{{]}.}\hlstd{parent}\hlsym{{-}$>$}\hlstd{rank}\hlsym{;}\\
250 \hlline{38\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{ranka}\hlsym{$>$}\hlstd{rankb}\hlsym{)\{}\\
251 \hlline{39\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlsym{(}\hlstd{S}\hlsym{{[}}\hlstd{b}\hlsym{{]}.}\hlstd{parent}\hlsym{){-}$>$}\hlstd{parent}\hlsym{=}\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]}.}\hlstd{parent}\hlsym{;}\\
252 \hlline{40\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]}.}\hlstd{rank}\hlsym{=}\hlstd{ranka}\hlsym{+}\hlstd{rankb}\hlsym{;}\\
253 \hlline{41\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
254 \hlline{42\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{else}\hlstd{}\hlsym{\{}\\
255 \hlline{43\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlsym{(}\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]}.}\hlstd{parent}\hlsym{){-}$>$}\hlstd{parent}\hlsym{=}\hlstd{S}\hlsym{{[}}\hlstd{b}\hlsym{{]}.}\hlstd{parent}\hlsym{;}\\
256 \hlline{44\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{S}\hlsym{{[}}\hlstd{b}\hlsym{{]}.}\hlstd{rank}\hlsym{=}\hlstd{ranka}\hlsym{+}\hlstd{rankb}\hlsym{;}\\
257 \hlline{45\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
258 \hlline{46\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
259 \hlline{47\ }\hlstd{\\
260 \hlline{48\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwc{inline\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{compress\textunderscore path}\hlstd{}\hlsym{(}\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{temp}\hlsym{,\ }\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{aux}\hlsym{,\ }\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{stop\textunderscore point}\hlsym{)\{}\\
261 \hlline{49\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{temp}\hlsym{=}\hlstd{aux}\hlsym{{-}$>$}\hlstd{parent}\hlsym{;}\\
262 \hlline{50\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{{-}$>$}\hlstd{parent}\hlsym{=}\hlstd{stop\textunderscore point}\hlsym{;}\\
263 \hlline{51\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{=}\hlstd{temp}\hlsym{;}\\
264 \hlline{52\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlsym{(}\hlstd{aux}\hlsym{!=}\hlstd{stop\textunderscore point}\hlsym{)\{}\\
265 \hlline{53\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{temp}\hlsym{=}\hlstd{aux}\hlsym{{-}$>$}\hlstd{parent}\hlsym{;}\\
266 \hlline{54\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{{-}$>$}\hlstd{parent}\hlsym{=}\hlstd{stop\textunderscore point}\hlsym{;}\\
267 \hlline{55\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{=}\hlstd{temp}\hlsym{;}\\
268 \hlline{56\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
269 \hlline{57\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
270 \hlline{58\ }\hlstd{\\
271 \hlline{59\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwc{inline\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{}\hlkwd{Find}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{a}\hlsym{)\{}\\
272 \hlline{60\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{temp}\hlsym{;}\\
273 \hlline{61\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{aux}\hlsym{;}\\
274 \hlline{62\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{result}\hlsym{;}\\
275 \hlline{63\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{=\&}\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]};}\\
276 \hlline{64\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{temp}\hlsym{=}\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]}.}\hlstd{parent}\hlsym{;}\\
277 \hlline{65\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlsym{(}\hlstd{aux}\hlsym{!=}\hlstd{temp}\hlsym{)\{}\\
278 \hlline{66\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{=}\hlstd{temp}\hlsym{;}\\
279 \hlline{67\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{temp}\hlsym{=({*}}\hlstd{aux}\hlsym{).}\hlstd{parent}\hlsym{;}\\
280 \hlline{68\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
281 \hlline{69\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{result}\hlsym{=}\hlstd{aux}\hlsym{{-}$>$}\hlstd{name}\hlsym{;}\\
282 \hlline{70\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{uf\textunderscore node}\hlsym{{*}\ }\hlstd{stop\textunderscore point}\hlsym{;}\\
283 \hlline{71\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{stop\textunderscore point}\hlsym{=}\hlstd{aux}\hlsym{;}\\
284 \hlline{72\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{aux}\hlsym{=\&}\hlstd{S}\hlsym{{[}}\hlstd{a}\hlsym{{]};}\\
285 \hlline{73\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{compress\textunderscore path}\hlstd{}\hlsym{(}\hlstd{temp}\hlsym{,}\hlstd{aux}\hlsym{,}\hlstd{stop\textunderscore point}\hlsym{);}\\
286 \hlline{74\ }\hlstd{\\
287 \hlline{75\ }}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{result}\hlsym{;}\\
288 \hlline{76\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
289 \hlline{77\ }\hlstd{}\hlsym{\};}\\
290 \hlline{78\ }\hlstd{}\\
291 \hlline{79\ }\hlkwc{class\ }\hlstd{edge}\hlsym{\{}\\
292 \hlline{80\ }\hlstd{}\hlkwc{public\ }\hlstd{}\hlsym{:}\\
293 \hlline{81\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{start}\hlsym{,}\hlstd{end}\hlsym{;}\\
294 \hlline{82\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{cost}\hlsym{;}\\
295 \hlline{83\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwc{inline\ }\hlstd{}\hlkwd{edge}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{start}\hlsym{,\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{end}\hlsym{,\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{cost}\hlsym{)\{}\\
296 \hlline{84\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{this}\hlstd{}\hlsym{{-}$>$}\hlstd{start}\hlsym{=}\hlstd{start}\hlsym{;}\\
297 \hlline{85\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{this}\hlstd{}\hlsym{{-}$>$}\hlstd{end}\hlsym{=}\hlstd{end}\hlsym{;}\\
298 \hlline{86\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{this}\hlstd{}\hlsym{{-}$>$}\hlstd{cost}\hlsym{=}\hlstd{cost}\hlsym{;}\\
299 \hlline{87\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
300 \hlline{88\ }\hlstd{}\hlsym{\};}\\
301 \hlline{89\ }\hlstd{}\\
302 \hlline{90\ }\\
303 \hlline{91\ }\hlkwc{inline\ }\hlstd{}\hlkwb{bool\ }\hlstd{}\hlkwd{compare}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{const\ }\hlstd{edge}\hlsym{\&\ }\hlstd{a}\hlsym{,}\hlstd{}\hlkwb{const\ }\hlstd{edge}\hlsym{\&\ }\hlstd{b\ }\hlsym{)\{}\\
304 \hlline{92\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{a}\hlsym{.}\hlstd{cost\ }\hlsym{$<$\ }\hlstd{b}\hlsym{.}\hlstd{cost}\hlsym{;}\\
305 \hlline{93\ }\hlstd{}\\
306 \hlline{94\ }\hlsym{\}}\\
307 \hlline{95\ }\hlstd{}\\
308 \hlline{96\ }\hlkwc{inline\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{bucket\textunderscore sort}\hlstd{}\hlsym{(}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\&\ }\hlstd{e}\hlsym{,}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{max\textunderscore cost}\hlsym{)\{}\\
309 \hlline{97\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{vector}\hlsym{$<$}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ $>$\ }\hlstd{}\hlkwd{v}\hlstd{}\hlsym{(}\hlstd{max\textunderscore cost}\hlsym{+}\hlstd{}\hlnum{1}\hlstd{}\hlsym{);}\\
310 \hlline{98\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlsym{(!}\hlstd{e}\hlsym{.}\hlstd{}\hlkwd{empty}\hlstd{}\hlsym{())\{}\\
311 \hlline{99\ }\hlstd{\ }\hlkwb{const\ }\hlstd{edge}\hlsym{\&\ }\hlstd{ed\ }\hlsym{=\ }\hlstd{e}\hlsym{.}\hlstd{}\hlkwd{front}\hlstd{}\hlsym{();}\\
312 \hlline{100\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{v}\hlsym{{[}}\hlstd{ed}\hlsym{.}\hlstd{cost}\hlsym{{]}.}\hlstd{}\hlkwd{push\textunderscore back}\hlstd{}\hlsym{(}\hlstd{ed}\hlsym{);}\\
313 \hlline{101\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{e}\hlsym{.}\hlstd{}\hlkwd{pop\textunderscore front}\hlstd{}\hlsym{();}\\
314 \hlline{102\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
315 \hlline{103\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{forn}\hlstd{}\hlsym{(}\hlstd{i}\hlsym{,\ }\hlstd{max\textunderscore cost}\hlsym{+}\hlstd{}\hlnum{1}\hlstd{}\hlsym{)\{}\\
316 \hlline{104\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlsym{(!}\hlstd{v}\hlsym{{[}}\hlstd{i}\hlsym{{]}.}\hlstd{}\hlkwd{empty}\hlstd{}\hlsym{())\{}\\
317 \hlline{105\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{e}\hlsym{.}\hlstd{}\hlkwd{push\textunderscore back}\hlstd{}\hlsym{(}\hlstd{v}\hlsym{{[}}\hlstd{i}\hlsym{{]}.}\hlstd{}\hlkwd{back}\hlstd{}\hlsym{());}\\
318 \hlline{106\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{v}\hlsym{{[}}\hlstd{i}\hlsym{{]}.}\hlstd{}\hlkwd{pop\textunderscore back}\hlstd{}\hlsym{();}\\
319 \hlline{107\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
320 \hlline{108\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
321 \hlline{109\ }\hlstd{}\\
322 \hlline{110\ }\hlsym{\}}\\
323 \hlline{111\ }\hlstd{}\\
324 \hlline{112\ }\hlkwc{inline\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{}\hlkwd{kruskal}\hlstd{}\hlsym{(}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\&\ }\hlstd{e}\hlsym{,}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{n}\hlsym{,\ }\hlstd{vector}\hlsym{$<$}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ $>$\&\ }\hlstd{adj}\hlsym{, }\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\&\ }\Righttorque\\
325 \hlline{113\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{rejected}\hlsym{,\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{max\textunderscore cost}\hlsym{,\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{m}\hlsym{)\{}\\
326 \hlline{114\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{union\textunderscore find\ }\hlkwd{list}\hlstd{}\hlsym{(}\hlstd{n}\hlsym{);}\\
327 \hlline{115\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{res}\hlsym{=}\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
328 \hlline{116\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{if\ }\hlstd{}\hlsym{(}\hlstd{m\ }\hlsym{$>$\ }\hlstd{max\textunderscore cost}\hlsym{)}\\
329 \hlline{117\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{bucket\textunderscore sort}\hlstd{}\hlsym{(}\hlstd{e}\hlsym{,}\hlstd{max\textunderscore cost}\hlsym{);}\\
330 \hlline{118\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{else}\\
331 \hlline{119\ }\hlstd{\ e}\hlsym{.}\hlstd{}\hlkwd{sort}\hlstd{}\hlsym{(}\hlstd{compare}\hlsym{);}\\
332 \hlline{120\ }\hlstd{\\
333 \hlline{121\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{foreachin}\hlstd{}\hlsym{(}\hlstd{it}\hlsym{,}\hlstd{e}\hlsym{)\{}\\
334 \hlline{122\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{s}\hlsym{,}\hlstd{f}\hlsym{;}\\
335 \hlline{123\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{s}\hlsym{=}\hlstd{it}\hlsym{{-}$>$}\hlstd{start}\hlsym{;}\\
336 \hlline{124\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{f}\hlsym{=}\hlstd{it}\hlsym{{-}$>$}\hlstd{end}\hlsym{;}\\
337 \hlline{125\ }\hlstd{\\
338 \hlline{126\ }}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{list}\hlsym{.}\hlstd{}\hlkwd{Find}\hlstd{}\hlsym{(}\hlstd{s}\hlsym{)==}\hlstd{list}\hlsym{.}\hlstd{}\hlkwd{Find}\hlstd{}\hlsym{(}\hlstd{f}\hlsym{))\{}\\
339 \hlline{127\ }\hlstd{\\
340 \hlline{128\ }}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{rejected}\hlsym{.}\hlstd{}\hlkwd{push\textunderscore back}\hlstd{}\hlsym{({*}}\hlstd{it}\hlsym{);}\\
341 \hlline{129\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{continue}\hlstd{}\hlsym{;}\\
342 \hlline{130\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
343 \hlline{131\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{else}\hlstd{}\hlsym{\{}\\
344 \hlline{132\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{list}\hlsym{.}\hlstd{}\hlkwd{Union}\hlstd{}\hlsym{(}\hlstd{s}\hlsym{,}\hlstd{f}\hlsym{);}\\
345 \hlline{133\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{res\ }\hlsym{+=\ }\hlstd{it}\hlsym{{-}$>$}\hlstd{cost}\hlsym{;}\\
346 \hlline{134\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{adj}\hlsym{{[}}\hlstd{s}\hlsym{{]}.}\hlstd{}\hlkwd{push\textunderscore back}\hlstd{}\hlsym{({*}}\hlstd{it}\hlsym{);}\\
347 \hlline{135\ }\hlstd{}\hlstd{\ \ \ \ \ }\hlstd{adj}\hlsym{{[}}\hlstd{f}\hlsym{{]}.}\hlstd{}\hlkwd{push\textunderscore back}\hlstd{}\hlsym{(}\hlstd{}\hlkwd{edge}\hlstd{}\hlsym{(}\hlstd{it}\hlsym{{-}$>$}\hlstd{end}\hlsym{,}\hlstd{it}\hlsym{{-}$>$}\hlstd{start}\hlsym{,}\hlstd{it}\hlsym{{-}$>$}\hlstd{cost}\hlsym{));}\\
348 \hlline{136\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
349 \hlline{137\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
350 \hlline{138\ }\hlstd{\\
351 \hlline{139\ }}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{res}\hlsym{;}\\
352 \hlline{140\ }\hlstd{}\\
353 \hlline{141\ }\hlsym{\}}\\
354 \hlline{142\ }\hlstd{}\\
355 \hlline{143\ }\\
356 \hlline{144\ }\hlkwc{inline\ }\hlstd{}\hlkwb{void\ }\hlstd{}\hlkwd{dfs\textunderscore fill\textunderscore max}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{u}\hlsym{,\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{x}\hlsym{,}\hlstd{vector}\hlsym{$<$}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ $>$\&\ }\hlstd{mst\textunderscore adjacency}\hlsym{,\ }\Righttorque\\
357 \hlline{145\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{vector}\hlsym{$<$}\hlstd{vector}\hlsym{$<$}\hlstd{edge}\hlsym{{*}$>$\ $>$\&\ }\hlstd{max}\hlsym{)\{}\\
358 \hlline{146\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{foreachin}\hlstd{}\hlsym{(}\hlstd{v}\hlsym{,(}\hlstd{mst\textunderscore adjacency}\hlsym{{[}}\hlstd{x}\hlsym{{]}))\{}\\
359 \hlline{147\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(\ }\hlstd{max}\hlsym{{[}}\hlstd{u}\hlsym{{]}{[}}\hlstd{v}\hlsym{{-}$>$}\hlstd{end}\hlsym{{]}\ ==\ }\hlstd{NULL\ }\hlsym{\&\&\ }\hlstd{v}\hlsym{{-}$>$}\hlstd{end\ }\hlsym{!=\ }\hlstd{u}\hlsym{)\{}\\
360 \hlline{148\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{x}\hlsym{==}\hlstd{u\ }\hlsym{\textbar \textbar \ }\hlstd{v}\hlsym{{-}$>$}\hlstd{cost\ }\hlsym{$>$\ }\hlstd{max}\hlsym{{[}}\hlstd{u}\hlsym{{]}{[}}\hlstd{x}\hlsym{{]}{-}$>$}\hlstd{cost}\hlsym{)\{}\\
361 \hlline{149\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{max}\hlsym{{[}}\hlstd{u}\hlsym{{]}{[}}\hlstd{v}\hlsym{{-}$>$}\hlstd{end}\hlsym{{]}\ =\ \&({*}}\hlstd{v}\hlsym{);}\\
362 \hlline{150\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
363 \hlline{151\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{else}\hlstd{}\hlsym{\{}\\
364 \hlline{152\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{max}\hlsym{{[}}\hlstd{u}\hlsym{{]}{[}}\hlstd{v}\hlsym{{-}$>$}\hlstd{end}\hlsym{{]}\ =\ }\hlstd{max}\hlsym{{[}}\hlstd{u}\hlsym{{]}{[}}\hlstd{x}\hlsym{{]};}\\
365 \hlline{153\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
366 \hlline{154\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{dfs\textunderscore fill\textunderscore max}\hlstd{}\hlsym{(}\hlstd{u}\hlsym{,}\hlstd{v}\hlsym{{-}$>$}\hlstd{end}\hlsym{,}\hlstd{mst\textunderscore adjacency}\hlsym{,}\hlstd{max}\hlsym{);}\\
367 \hlline{155\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
368 \hlline{156\ }\hlstd{\\
369 \hlline{157\ }}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
370 \hlline{158\ }\hlstd{}\hlsym{\}}\\
371 \hlline{159\ }\hlstd{}\\
372 \hlline{160\ }\hlkwc{inline\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{}\hlkwd{second\textunderscore mst}\hlstd{}\hlsym{(}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{n}\hlsym{,\ }\hlstd{vector}\hlsym{$<$}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ $>$\&\ }\hlstd{mst\textunderscore adjacency}\hlsym{,\ }\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\&\ }\Righttorque\\
373 \hlline{161\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{rejected}\hlsym{,}\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{minimum\textunderscore cost}\hlsym{)\{}\\
374 \hlline{162\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{vector}\hlsym{$<$}\hlstd{vector}\hlsym{$<$}\hlstd{edge}\hlsym{{*}$>$\ $>$\ }\hlstd{}\hlkwd{max}\hlstd{}\hlsym{(}\hlstd{n}\hlsym{,}\hlstd{vector}\hlsym{$<$}\hlstd{edge}\hlsym{{*}$>$(}\hlstd{n}\hlsym{,}\hlstd{NULL}\hlsym{));}\\
375 \hlline{163\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$::}\hlstd{iterator\ it\ }\hlsym{=\ }\hlstd{rejected}\hlsym{.}\hlstd{}\hlkwd{begin}\hlstd{}\hlsym{();}\\
376 \hlline{164\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{start\ }\hlsym{=\ }\hlstd{it}\hlsym{{-}$>$}\hlstd{start}\hlsym{;}\\
377 \hlline{165\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{end\ }\hlsym{=\ }\hlstd{it}\hlsym{{-}$>$}\hlstd{end}\hlsym{;}\\
378 \hlline{166\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{dfs\textunderscore fill\textunderscore max}\hlstd{}\hlsym{(}\hlstd{start}\hlsym{,}\hlstd{start}\hlsym{,}\hlstd{mst\textunderscore adjacency}\hlsym{,\ }\hlstd{max}\hlsym{);}\\
379 \hlline{167\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{old\textunderscore cost}\hlsym{=}\hlstd{max}\hlsym{{[}}\hlstd{start}\hlsym{{]}{[}}\hlstd{end}\hlsym{{]}{-}$>$}\hlstd{cost}\hlsym{;}\\
380 \hlline{168\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{new\textunderscore cost}\hlsym{=}\hlstd{it}\hlsym{{-}$>$}\hlstd{cost}\hlsym{;}\\
381 \hlline{169\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{minimum}\hlsym{=\ }\hlstd{new\textunderscore cost\ }\hlsym{{-}\ }\hlstd{old\textunderscore cost}\hlsym{;}\\
382 \hlline{170\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{it}\hlsym{++;}\\
383 \hlline{171\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{for}\hlstd{}\hlsym{(}\hlstd{it}\hlsym{;}\hlstd{it\ }\hlsym{!=\ }\hlstd{rejected}\hlsym{.}\hlstd{}\hlkwd{end}\hlstd{}\hlsym{();}\hlstd{it}\hlsym{++\ )\{}\\
384 \hlline{172\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{start\ }\hlsym{=\ }\hlstd{it}\hlsym{{-}$>$}\hlstd{start}\hlsym{;}\\
385 \hlline{173\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{end\ }\hlsym{=\ }\hlstd{it}\hlsym{{-}$>$}\hlstd{end}\hlsym{;}\\
386 \hlline{174\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{max}\hlsym{{[}}\hlstd{start}\hlsym{{]}{[}}\hlstd{end}\hlsym{{]}\ ==\ }\hlstd{NULL}\hlsym{)}\hlstd{}\hlkwd{dfs\textunderscore fill\textunderscore max}\hlstd{}\hlsym{(}\hlstd{start}\hlsym{,}\hlstd{start}\hlsym{,}\hlstd{mst\textunderscore adjacency}\hlsym{,\ }\hlstd{max}\hlsym{);}\\
387 \hlline{175\ }\hlstd{\\
388 \hlline{176\ }}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{aux}\hlsym{=}\hlstd{it}\hlsym{{-}$>$}\hlstd{cost}\hlsym{{-}\ }\hlstd{max}\hlsym{{[}}\hlstd{start}\hlsym{{]}{[}}\hlstd{end}\hlsym{{]}{-}$>$}\hlstd{cost}\hlsym{;}\\
389 \hlline{177\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{aux\ }\hlsym{$<$\ }\hlstd{minimum}\hlsym{)\{}\\
390 \hlline{178\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{aux\ }\hlsym{==\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{)}\hlstd{}\hlkwa{return\ }\hlstd{minimum\textunderscore cost}\hlsym{;}\\
391 \hlline{179\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{minimum}\hlsym{=\ }\hlstd{aux}\hlsym{;}\\
392 \hlline{180\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{new\textunderscore cost\ }\hlsym{=}\hlstd{\ \ }\hlsym{}\hlstd{it}\hlsym{{-}$>$}\hlstd{cost}\hlsym{;}\\
393 \hlline{181\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ \ \ \ \ }\hlstd{old\textunderscore cost\ }\hlsym{=\ }\hlstd{max}\hlsym{{[}}\hlstd{start}\hlsym{{]}{[}}\hlstd{end}\hlsym{{]}{-}$>$}\hlstd{cost}\hlsym{;}\\
394 \hlline{182\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlsym{\}}\\
395 \hlline{183\ }\hlstd{\\
396 \hlline{184\ }}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
397 \hlline{185\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{return\ }\hlstd{minimum\textunderscore cost\ }\hlsym{{-}\ }\hlstd{old\textunderscore cost\ }\hlsym{+\ }\hlstd{new\textunderscore cost}\hlsym{;}\\
398 \hlline{186\ }\hlstd{}\hlsym{\}}\\
399 \hlline{187\ }\hlstd{}\\
400 \hlline{188\ }\\
401 \hlline{189\ }\hlkwb{int\ }\hlstd{}\hlkwd{main}\hlstd{}\hlsym{()\{}\\
402 \hlline{190\ }\hlstd{}\hlkwb{int\ }\hlstd{cases}\hlsym{;}\\
403 \hlline{191\ }\hlstd{cin}\hlsym{$>$$>$}\hlstd{cases}\hlsym{;}\\
404 \hlline{192\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{n}\hlsym{;}\\
405 \hlline{193\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{m}\hlsym{;}\\
406 \hlline{194\ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{from}\hlsym{,}\hlstd{to}\hlsym{,}\hlstd{cost}\hlsym{;}\\
407 \hlline{195\ }\hlstd{}\\
408 \hlline{196\ }\\
409 \hlline{197\ }\hlkwa{while}\hlstd{}\hlsym{(}\hlstd{cases\ }\hlsym{$>$\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{)\{}\\
410 \hlline{198\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwd{scanf}\hlstd{}\hlsym{(}\hlstd{}\hlstr{``\%hu\ \%hu''}\hlstd{}\hlsym{,\&}\hlstd{n}\hlsym{,\&}\hlstd{m}\hlsym{);}\\
411 \hlline{199\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{vector}\hlsym{$<$}\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ $>$\ }\hlstd{}\hlkwd{mst\textunderscore adjacency}\hlstd{}\hlsym{(}\hlstd{n}\hlsym{);}\\
412 \hlline{200\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ }\hlstd{rejected}\hlsym{;}\\
413 \hlline{201\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{list}\hlsym{$<$}\hlstd{edge}\hlsym{$>$\ }\hlstd{l}\hlsym{;}\\
414 \hlline{202\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{cant\textunderscore edges}\hlsym{=}\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
415 \hlline{203\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{max\textunderscore cost\ }\hlsym{=\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
416 \hlline{204\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwa{while}\hlstd{}\hlsym{(}\hlstd{cant\textunderscore edges\ }\hlsym{$<$\ }\hlstd{m}\hlsym{)\{}\\
417 \hlline{205\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwd{scanf}\hlstd{}\hlsym{(}\hlstd{}\hlstr{``\%hu\ \%hu\ \%hu''}\hlstd{}\hlsym{,\&}\hlstd{from}\hlsym{,\&}\hlstd{to}\hlsym{,\&}\hlstd{cost}\hlsym{);}\\
418 \hlline{206\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{}\hlkwa{if}\hlstd{}\hlsym{(}\hlstd{cost\ }\hlsym{$>$\ }\hlstd{max\textunderscore cost}\hlsym{)}\hlstd{max\textunderscore cost\ }\hlsym{=\ }\hlstd{cost}\hlsym{;}\\
419 \hlline{207\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{l}\hlsym{.}\hlstd{}\hlkwd{push\textunderscore back}\hlstd{}\hlsym{(}\hlstd{}\hlkwd{edge}\hlstd{}\hlsym{(}\hlstd{from}\hlsym{{-}}\hlstd{}\hlnum{1}\hlstd{}\hlsym{,}\hlstd{to}\hlsym{{-}}\hlstd{}\hlnum{1}\hlstd{}\hlsym{,}\hlstd{cost}\hlsym{));}\\
420 \hlline{208\ }\hlstd{}\hlstd{\ \ \ \ \ \ \ \ }\hlstd{cant\textunderscore edges}\hlsym{++;}\\
421 \hlline{209\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlsym{\}}\\
422 \hlline{210\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlkwb{unsigned\ short\ }\hlstd{k\ }\hlsym{=}\hlstd{}\hlkwd{kruskal}\hlstd{}\hlsym{(}\hlstd{l}\hlsym{,}\hlstd{n}\hlsym{,}\hlstd{mst\textunderscore adjacency}\hlsym{,}\hlstd{rejected}\hlsym{,}\hlstd{max\textunderscore cost}\hlsym{,}\hlstd{m}\hlsym{);}\\
423 \hlline{211\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{}\hlslc{//unsigned\ short\ k\ =kruskal(l,n,mst\textunderscore adjacency,rejected);}\\
424 \hlline{212\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{cout\ }\hlsym{$<$$<$\ }\hlstd{k}\hlsym{$<$$<$}\hlstd{}\hlstr{``\ ''}\hlstd{}\hlsym{;}\\
425 \hlline{213\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{cout\ }\hlsym{$<$$<$\ }\hlstd{}\hlkwd{second\textunderscore mst}\hlstd{}\hlsym{(}\hlstd{n}\hlsym{,}\hlstd{mst\textunderscore adjacency}\hlsym{,}\hlstd{rejected}\hlsym{,}\hlstd{k}\hlsym{)$<$$<$}\hlstd{endl}\hlsym{;}\\
426 \hlline{214\ }\hlstd{}\hlstd{\ \ \ \ }\hlstd{cases}\hlsym{{-}{-};}\\
427 \hlline{215\ }\hlstd{}\hlsym{\}}\\
428 \hlline{216\ }\hlstd{}\hlkwa{return\ }\hlstd{}\hlnum{0}\hlstd{}\hlsym{;}\\
429 \hlline{217\ }\hlstd{}\hlsym{\}}\\
430 \hlline{218\ }\hlstd{}\\
431 \hlline{219\ }\\
432 \hlline{220\ }\\
433 \hlline{221\ }\\
434 \hlline{222\ }\\
435 \hlline{223\ }\\
436 \mbox{}
437 \normalfont
438 \shorthandon{"}